package com.ruoyi.flow.config;

import com.alibaba.druid.support.http.WebStatFilter;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.fasterxml.jackson.core.PrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.flow.core.CorsFilter;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.Formatter;
import org.springframework.format.support.FormattingConversionService;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.validation.Validator;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.servlet.resource.ResourceUrlProvider;
import org.springframework.web.servlet.view.freemarker.FreeMarkerView;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * SpringMVC 配置信息
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
    @Override
    protected void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        Map<String, MediaType> mediaTypes = Maps.newHashMap();
        mediaTypes.put("xml", MediaType.APPLICATION_XML);
        mediaTypes.put("json", MediaType.APPLICATION_JSON);
        mediaTypes.put("html", MediaType.TEXT_HTML);
        configurer.mediaTypes(mediaTypes);
        configurer.ignoreAcceptHeader(true);
        configurer.favorParameter(false);
        configurer.defaultContentType(MediaType.APPLICATION_JSON);
    }

    @Override
    /**
     * 配置视图解析器为freemarker
     */
    protected void configureViewResolvers(ViewResolverRegistry registry) {
        registry.enableContentNegotiation(new FreeMarkerView());
        registry.viewResolver(new FreeMarkerViewResolver());
        registry.enableContentNegotiation(false);
    }

    @Override
    /**
     * 定义无Controller的path<->view直接映射
     */
    protected void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("redirect:/index");
    }

    @Override
    /**
     * 添加静态资源
     */
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        //静态资源映射
        registry.addResourceHandler("/assets/**").addResourceLocations("/assets/").setCachePeriod(31536000);
    }

    @Override
    /**
     * 添加FastJsonHttpMessageConverter
     */
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        converter.setSupportedMediaTypes(Lists.newArrayList(MediaType.APPLICATION_JSON));
        converters.add(converter);

        converters.add(customerMappingJackson2HttpMessageConverter());
        converters.add(stringHttpMessageConverter());
    }

    @Bean
    public MappingJackson2HttpMessageConverter customerMappingJackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        messageConverter.setObjectMapper(new ObjectMapper());
        messageConverter.setPrettyPrint(false);
        return messageConverter;
    }

    @Bean
    public StringHttpMessageConverter stringHttpMessageConverter() {
        StringHttpMessageConverter stringConvert = new StringHttpMessageConverter();
        List<MediaType> stringMediaTypes = new ArrayList<MediaType>() {{
            add(new MediaType("text", "plain", Charset.forName("UTF-8")));
        }};
        stringConvert.setSupportedMediaTypes(stringMediaTypes);
        return stringConvert;
    }

    @Override
    protected void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean(name = "coreFilter")
    public CorsFilter corsFilter(){
        return new CorsFilter();
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new DelegatingFilterProxy());
        registrationBean.addUrlPatterns("/*");
        //被代理filter
        registrationBean.addInitParameter("targetBeanName", "coreFilter");
        //指明作用于filter的所有生命周期
        registrationBean.addInitParameter("targetFilterLifecycle", "true");
        registrationBean.setName("corsFilter");
        registrationBean.setOrder(1);
        registrationBean.setEnabled(false);
        return registrationBean;
    }



}
